package com.tencent.bugly.beta.ui;

import android.app.Activity;
import android.app.ActivityManager;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.tencent.bugly.proguard.an;
import com.tencent.bugly.proguard.aq;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/* compiled from: BUGLY */
/* loaded from: classes.dex */
public class g {
    public static final Map<Integer, b> a = new ConcurrentHashMap();
    public static final Map<Integer, com.tencent.bugly.beta.global.d> b = new ConcurrentHashMap();
    public static final Map<Integer, com.tencent.bugly.beta.global.d> c = new ConcurrentHashMap();
    private static com.tencent.bugly.beta.global.d d;

    public static synchronized void a(b bVar, boolean z) {
        synchronized (g.class) {
            if (Build.VERSION.SDK_INT >= 14) {
                a(bVar, z, false, DefaultRenderersFactory.DEFAULT_ALLOWED_VIDEO_JOINING_TIME_MS);
            } else {
                try {
                    ActivityManager activityManager = (ActivityManager) com.tencent.bugly.beta.global.e.E.s.getSystemService("activity");
                    if (activityManager != null) {
                        activityManager.getRunningTasks(1);
                        a(bVar, z, false, DefaultRenderersFactory.DEFAULT_ALLOWED_VIDEO_JOINING_TIME_MS);
                    }
                } catch (SecurityException unused) {
                    if (z) {
                        a(bVar, z, true, 0L);
                        return;
                    }
                    an.e("无法获取GET_TASK权限，将在通知栏提醒升级，如需弹窗提醒，请在AndroidManifest.xml中添加GET_TASKS权限：\n<uses-permission android:name=\"android.permission.GET_TASKS\" />\n", new Object[0]);
                    if (com.tencent.bugly.beta.upgrade.c.a.b != null && com.tencent.bugly.beta.upgrade.c.a.b.a != null) {
                        c.a.a(com.tencent.bugly.beta.upgrade.c.a.b.a, bVar);
                    }
                }
            }
        }
    }

    public static synchronized void a(b bVar, boolean z, boolean z2, long j) {
        synchronized (g.class) {
            if (bVar != null) {
                if (!bVar.b()) {
                    int hashCode = bVar.hashCode();
                    if (bVar instanceof h) {
                        com.tencent.bugly.beta.utils.e.b(d);
                        if (((h) bVar).p.g == 2) {
                            d = new com.tencent.bugly.beta.global.d(15, bVar, Boolean.valueOf(z), Boolean.valueOf(z2), Long.valueOf(j));
                            com.tencent.bugly.beta.utils.e.a(d, 3000L);
                        }
                        if (!z && !b()) {
                            com.tencent.bugly.beta.global.d dVar = b.get(Integer.valueOf(hashCode));
                            if (dVar == null) {
                                dVar = new com.tencent.bugly.beta.global.d(11, bVar, Boolean.valueOf(z), Boolean.valueOf(z2), Long.valueOf(j));
                                b.put(Integer.valueOf(hashCode), dVar);
                            }
                            com.tencent.bugly.beta.utils.e.b(dVar);
                            com.tencent.bugly.beta.utils.e.a(dVar, j);
                            return;
                        }
                        com.tencent.bugly.beta.utils.e.b(b.remove(Integer.valueOf(hashCode)));
                    }
                    if (!z2 && !aq.b(com.tencent.bugly.beta.global.e.E.s)) {
                        com.tencent.bugly.beta.global.d dVar2 = c.get(Integer.valueOf(hashCode));
                        if (dVar2 == null) {
                            dVar2 = new com.tencent.bugly.beta.global.d(11, bVar, Boolean.valueOf(z), Boolean.valueOf(z2), Long.valueOf(j));
                            c.put(Integer.valueOf(hashCode), dVar2);
                        }
                        com.tencent.bugly.beta.utils.e.b(dVar2);
                        com.tencent.bugly.beta.utils.e.a(dVar2, j);
                        return;
                    }
                    com.tencent.bugly.beta.utils.e.b(c.remove(Integer.valueOf(hashCode)));
                    com.tencent.bugly.beta.global.d dVar3 = new com.tencent.bugly.beta.global.d(17, a, Integer.valueOf(hashCode), bVar);
                    FragmentActivity activity = bVar.getActivity();
                    if (activity != null) {
                        if (activity instanceof BetaActivity) {
                            ((BetaActivity) activity).onDestroyRunnable = dVar3;
                        } else {
                            com.tencent.bugly.beta.utils.e.a(dVar3, 400L);
                        }
                        activity.finish();
                    } else {
                        dVar3.run();
                    }
                }
            }
        }
    }

    public static String a() {
        List<ActivityManager.RunningTaskInfo> runningTasks;
        try {
            if (Build.VERSION.SDK_INT >= 14) {
                return com.tencent.bugly.crashreport.common.info.a.b().v;
            }
            ActivityManager activityManager = (ActivityManager) com.tencent.bugly.beta.global.e.E.s.getSystemService("activity");
            if (activityManager == null || (runningTasks = activityManager.getRunningTasks(1)) == null || runningTasks.isEmpty()) {
                return null;
            }
            return runningTasks.get(0).topActivity.getClassName();
        } catch (SecurityException unused) {
            an.e("无法获取Activity信息，请在AndroidManifest.xml中添加GET_TASKS权限：\n<uses-permission android:name=\"android.permission.GET_TASKS\" />\n", new Object[0]);
            return null;
        } catch (Exception e) {
            if (an.b(e)) {
                return null;
            }
            e.printStackTrace();
            return null;
        }
    }

    public static synchronized boolean b() {
        synchronized (g.class) {
            String a2 = a();
            if (a2 == null || a2.equals("background") || a2.equals("unknown")) {
                return false;
            }
            Class<?> cls = null;
            try {
                cls = Class.forName(a2);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (!com.tencent.bugly.beta.global.e.E.m.isEmpty()) {
                for (Class<? extends Activity> cls2 : com.tencent.bugly.beta.global.e.E.m) {
                    if (TextUtils.equals(cls2.getName(), a2) || (cls != null && cls2.isAssignableFrom(cls))) {
                        return true;
                    }
                }
                return false;
            } else if (com.tencent.bugly.beta.global.e.E.n.isEmpty()) {
                return true;
            } else {
                for (Class<? extends Activity> cls3 : com.tencent.bugly.beta.global.e.E.n) {
                    if (TextUtils.equals(cls3.getName(), a2) || (cls != null && cls3.isAssignableFrom(cls))) {
                        return false;
                    }
                }
                return true;
            }
        }
    }
}
